﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using Lamex.Interface;

namespace Lamex.mSchedule
{
    public partial class frmSCH_Position_Search_Table : frmBaseChild
    {
        public frmSCH_Position_Search_Table()
        {
            InitializeComponent();
        }

        private void frmSCH_Position_Search_Table_Load(object sender, EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
        }

        private void frmSCH_Position_Search_Table_Resize(object sender, EventArgs e)
        {
            panelBottom.Height = this.Height - panelTop.Height;
            panelTop_Right.Width = panelTop.Width - panelTop_Left.Width;
        }

        private void sbtnView_Click(object sender, EventArgs e)
        {
            string strQuery = "";

            if (radioManufacture_No.Checked == true)
            {
                strQuery = " WHERE MO号码 = ";
            }
            else if (radioFollow_No.Checked == true)
            {
                strQuery = " WHERE 跟进表号 = ";
            }
            else if (radioBarcode.Checked == true)
            {
                strQuery = " WHERE 条码编号 = ";
            }

            strQuery = strQuery + "'" + textBox1.Text.ToString().Trim() + "'";
            setGridData_Details(strQuery);

            //加载步骤信息
            string[] arrayStr = new string[5];

            arrayStr[0] = "M01_剪板";
            arrayStr[1] = "M03_机械冲";
            arrayStr[2] = "M04_数控折弯";
            arrayStr[3] = "M05_焊接组装线";
            arrayStr[4] = "M07_组装包装";
            ucStep1.Steps = arrayStr;
            ucStep1.StepIndex = 4;
        }

        private void setGridData_Details(string strQuery)
        {
            string StrSql = @"SELECT * 
                            FROM OP_Follow_Process_Flow" + strQuery;

            DataTable dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(StrSql);

            if (dt.Rows.Count > 0)
            {
                gridControl1.DataSource = dt;
                //setGridViewColumnAutoWidth(gridView1);

                gridView1.OptionsBehavior.Editable = false;
                gridView1.OptionsView.ColumnAutoWidth = true;
                gridView1.Columns[0].BestFit();
                gridView1.Columns[1].BestFit();

                //gridView3.BestFitColumns();
                //setGridViewColumnAutoWidth();

                //refreshPositionStaduss();
            }
        }

        private void refreshPositionStaduss()
        {
            //string str="";
            for (int i = 1; i < gridView1.RowCount; i++)
            {
                for (int j = 1; j < gridView1.Columns.Count; j++)
                {
                    {
                        //str = gridView1.GetRowCellValue(0, gridView1.Columns[0]);
                        //string str = gridView1.GetRowCellValue(0, gridView1.Columns[j]];
                        //gridView1.CELLS
                        //gridView1.rows[4].cells[3].backcolor = Color.Red;
                    }
                }
            }
        }

        private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {         
            //GridView View = sender as GridView;
            if (e.Column.FieldName == "MO号码")//设背景
            {
                //int pointID = (gridView1.GetRow(e.RowHandle) as object).BgColor;
                e.Appearance.BackColor = Color.AliceBlue;
            }
            else if (e.Column.FieldName == "跟进表号")//设渐变背景
            {
                //int pointID = (gridView1.GetRow(e.RowHandle) as object).BgColor;
                e.Appearance.BackColor = Color.AliceBlue;
                e.Appearance.BackColor2 = Color.FromArgb(255, 255, 255);
            }
            else if (e.Column.FieldName == "条码编号")//设字体颜色
            {
                //int pointID = (gridView1.GetRow(e.RowHandle) as object).BgColor;
                e.Appearance.ForeColor = Color.Red;//Color.FromArgb(42, 173, 231);
            }

            if (e.Column.FieldName.Substring(3,1) == "_")
            {
                DataRow dr = gridView1.GetDataRow(e.RowHandle);
                string strTemp = dr[e.Column.FieldName].ToString().Trim();
                switch (strTemp)
                {
                    case "":   //工序不适用
                        e.Appearance.BackColor = Color.LightGray;//Color.FromArgb(42, 173, 231);
                        e.Appearance.ForeColor = Color.LightGray;//Color.FromArgb(42, 173, 231);
                        break;
                    case "Y":   //工序已完成
                        e.Appearance.BackColor = Color.LightGreen;//Color.FromArgb(42, 173, 231);
                        e.Appearance.ForeColor = Color.LightGreen;//Color.FromArgb(42, 173, 231);
                        break;
                    case "O":   //工序进行中
                        e.Appearance.BackColor = Color.LightGoldenrodYellow;//Color.FromArgb(42, 173, 231);
                        e.Appearance.ForeColor = Color.LightGoldenrodYellow;//Color.FromArgb(42, 173, 231);
                        break;
                    case "N":   //工序未完成
                        e.Appearance.BackColor = Color.LightPink;//Color.FromArgb(42, 173, 231);
                        e.Appearance.ForeColor = Color.LightPink;//Color.FromArgb(42, 173, 231);
                        break;
                }
            }

            ////改变行的效果
            //DevExpress.Utils.AppearanceDefault appNotPass1 = new DevExpress.Utils.AppearanceDefault(Color.Black, Color.Salmon, Color.Empty, Color.SeaShell, System.Drawing.Drawing2D.LinearGradientMode.Horizontal);
            //DevExpress.Utils.AppearanceDefault appNotPass2 = new DevExpress.Utils.AppearanceDefault(Color.Black, Color.Yellow, Color.Empty, Color.SeaShell, System.Drawing.Drawing2D.LinearGradientMode.Horizontal);
 
            //DataRow dr = gridView2.GetDataRow(e.RowHandle);
            //if (dr != null)
            //{
            //    if (dr["sex"].ToString() == "女" && dr["birthday1"].ToString().Trim() == "55")
            //        DevExpress.Utils.AppearanceHelper.Apply(e.Appearance, appNotPass2);
            //    else if (dr["sex"].ToString() == "男" && dr["birthday1"].ToString().Trim() == "60")
            //        DevExpress.Utils.AppearanceHelper.Apply(e.Appearance, appNotPass1);
            //}
   
            //改变单元格的应用
            DevExpress.Utils.AppearanceDefault appNotPass1 = new DevExpress.Utils.AppearanceDefault(Color.Black, Color.Salmon, Color.Empty, Color.SeaShell, System.Drawing.Drawing2D.LinearGradientMode.Horizontal);
            DevExpress.Utils.AppearanceDefault appNotPass2 = new DevExpress.Utils.AppearanceDefault(Color.Black, Color.Yellow, Color.Empty, Color.SeaShell, System.Drawing.Drawing2D.LinearGradientMode.Horizontal);
            if (e.Column.FieldName == "sex")
            {
                DataRow dr = gridView1.GetDataRow(e.RowHandle);
                string strTemp = dr[e.Column.FieldName].ToString().Trim();
                if (!string.IsNullOrEmpty(strTemp))
                {
                    switch (strTemp)
                    {
                        case "男":
                            DevExpress.Utils.AppearanceHelper.Apply(e.Appearance, appNotPass1);
                            break;
                        case "女":
                            DevExpress.Utils.AppearanceHelper.Apply(e.Appearance, appNotPass2);
                            break;
                    }
                }
            }
        }

        public override void DoView(IButtonInfo sender)
        {
            sbtnView_Click(null, null);
            base.DoView(sender);
        }
    }
}